package com.spider.admin.controller;

import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.service.app.AppService;
import com.spider.admin.store.primary.model.AppInfo;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    @GetMapping("/query/{pid}")
    public AppInfo queryAppById(@PathVariable @NotEmpty(message = "查询ID不能为空") String pid) throws SpiderException {
        return appService.queryAppById(pid);
    }

    @GetMapping("/list/{tenantId}")
    public PageResult<AppInfo> queryAppListByTenant(@PathVariable @NotEmpty(message = "查询租户ID不能为空") String tenantId,
                                                    @RequestParam(required = false) Integer page,
                                                    @RequestParam(required = false) Integer pageSize,
                                                    @RequestParam(required = false) String appName,
                                                    @RequestParam(required = false) String cloudId,
                                                    @RequestParam(required = false) String appStatus)throws SpiderException{
        return  appService.queryAppByTenantId(tenantId, page, pageSize, appName, cloudId,appStatus);
    }

    @PostMapping("/add")
    public int saveApp(@RequestBody @Validated(AppInfo.Save.class) AppInfo appInfo) throws SpiderException {
        return appService.saveApp(appInfo);
    }

    @PutMapping("/update/{pid}")
    public int updateAppById(@RequestBody @Validated(AppInfo.Update.class) AppInfo appInfo, @PathVariable @NotEmpty(message = "更新条件ID不能为空") String pid) throws SpiderException {
        return appService.updateApp(appInfo, pid);
    }

    @PutMapping("/active/{pid}")
    public int activeAppById(@PathVariable @NotEmpty(message = "激活条件ID不能为空") String pid) throws SpiderException {
        return appService.activeApp(pid);
    }

    @PutMapping("/archive/{pid}")
    public int archiveAppById(@PathVariable @NotEmpty(message = "归档条件ID不能为空") String pid) throws SpiderException {
        return appService.archiveApp(pid);
    }
}

